//+------------------------------------------------------------------+
//|                                                   PipeClient.mq4 |
//|                   Copyright 2012-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "Copyright 2012-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property version     "1.00"
#property description "Pipe client sample using CFilePipe standard class"
#property strict

#include <Files\FilePipe.mqh>

extern int clientSleep=1000;

string Connection();

CFilePipe ExtPipe;
void OnInit()
  {
//---
   
   
   while(true){
   
   
   string msg = Connection();
   Print(msg);
   string result[];
   SplitString(msg,result);
   
   for (int i=0; i<ArraySize(result); i++){
   Print("result[",i,"]=",result[i]);
   }
   
   }
   
 }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{


}
//+------------------------------------------------------------------+


string Connection() {

  string str="";

//--- wait for pipe server
   bool bfirst=true;
   while(!IsStopped())
     {
      if(ExtPipe.Open("\\\\.\\pipe\\MQL5.Pipe",FILE_READ|FILE_WRITE|FILE_BIN)!=INVALID_HANDLE)
         break;
      if(bfirst)
        {
         bfirst=false;
         Print("Client: waiting for pipe server");
        }
      Sleep(clientSleep);
     }
   if(IsStopped())
      return("");
   //Print("Client: pipe opened");
   
//--- send welcome message
/*
 if(!ExtPipe.WriteString(__FILE__+" on MQL4 build "+IntegerToString(__MQL4BUILD__)))
     {
      Print("Client: sending welcome message failed");
      return;
     }
     */
    
//--- read data from server
   
   int    value=0;

   if(!ExtPipe.ReadString(str))
     {
      Print("Client: getting string from server failed");
      return("");
     }
   Print("Server: \"",str,"\" received");
/*
 if(!ExtPipe.ReadInteger(value))
     {
      Print("Client: reading integer failed");
      return;
     }
   Print("Server: ",value," received");
   
//--- send data to server

   if(!ExtPipe.WriteString("Test string"))
     {
      Print("Client: sending string failed");
      return;
     }

  if(!ExtPipe.WriteInteger(value))
     {
      Print("Client: sending integer failed");
      return;
     }
*/
   ExtPipe.Close();
   
   return(str);
}

void SplitString[](string to_split, string& result[]){
   string sep=";";                // A separator as a character
   ushort u_sep;                  // The code of the separator character
   //--- Get the separator code
   u_sep=StringGetCharacter(sep,0);
   //--- Split the string to substrings
   int k=StringSplit(to_split,u_sep,result);
   //--- Show a comment 
   // PrintFormat("Strings obtained: %d. Used separator '%s' with the code %d",k,sep,u_sep);
   //--- Now output all obtained strings
   /*if(k>0)
     {
      for(int i=0;i<k;i++)
        {
         PrintFormat("result[%d]=%s",i,result[i]);
        }
     }
   */
 }